package com.six.clinic.common.custom_enum;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: shinan
 * @Version: 1.8
 * @create: 2020/11/30 20:00
 * @Description: 接诊状态
 */
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
@Getter
@AllArgsConstructor
public enum MeetDoctorStatus {

    /**
     * 接诊中
     */
    IS_MEET_DOCTOR(0, "接诊中"),

    /**
     * 待接诊
     */
    WAIT_MEET_DOCTOR(1, "待接诊"),

    /**
     * 已接诊
     */
    HAS_MEET_DOCTOR(2, "已接诊");

    private final Integer value;
    private final String status;

    /**
     * 用于保存所有的枚举值
     */
    private static Map<String, MeetDoctorStatus> RESOURCE_MAP = Stream
            .of(MeetDoctorStatus.values())
            .collect(Collectors.toMap(s -> s.getStatus(), Function.identity()));

    /**
     * 枚举反序列话调用该方法
     * @param jsonNode
     * @return
     */
    @JsonCreator
    public static MeetDoctorStatus des(final JsonNode jsonNode) {
        return Optional
                .ofNullable(RESOURCE_MAP.get(jsonNode.get("status").asText()))
                .orElseThrow(() -> new IllegalArgumentException(jsonNode.get("status").asText()));
    }


    public static MeetDoctorStatus getEnum(Integer value) {
        for (MeetDoctorStatus meetDoctorStatus : MeetDoctorStatus.values()) {
            if (meetDoctorStatus.getValue().equals(value)) {
                return meetDoctorStatus;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return String.valueOf(this.value);
    }
}
